package uz.edo.app.admin.userPages;

import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.admin.AdminPage;
import uz.edo.db.bean.Department;
import uz.edo.db.bean.Role;
import uz.edo.db.bean.User;
import uz.edo.db.manager.DepartmentManager;
import uz.edo.db.manager.RoleManager;
import uz.edo.db.manager.UserManager;
import uz.edo.utils.Constants;

import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/24/12
 * Time: 12:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserAdd extends AdminPage{

    private UserManager manager = UserManager.getInstance();
    private RoleManager roleManager = RoleManager.getInstance();

    public UserAdd() throws Exception {
        final User user = new User();
        CompoundPropertyModel model = new CompoundPropertyModel(user);
        final CheckBox active = new CheckBox("active");

        Form form = new Form("form",model){
            @Override
            protected void onSubmit() {
                try {
                    user.setStartDate(new Date());
                    manager.saveOrUpdate(user);
                    setResponsePage(UserListView.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        add(form);
        form.add(new TextField("userName"));
        form.add(new TextField("password"));
        form.add(active);
        form.add(new ListMultipleChoice<Role>("roleList", isPrivileged("su") ? roleManager.getAll() : roleManager.getRoles(), Constants.RENDER));
        form.add(new DropDownChoice<Department>("department", DepartmentManager.getInstance().getAll(), Constants.RENDER_UZ));
    }
}
